﻿package {
	import alternativa.engine3d.core.Mesh;
	import alternativa.engine3d.materials.FillMaterial;
	import alternativa.engine3d.materials.WireMaterial;
	
	public class House extends Mesh {
		// Dimensions of the house
		protected var houseWidth:Number = 60;
		protected var houseLength:Number = 100;
		protected var wallsHeight:int = 60;
		protected var roofBottomWidth:int = 80;
		protected var roofBottomLength:int = 120;
		protected var roofTopWidth:int = 10;
		protected var roofTopLength:int = 60;
		protected var roofHeight:int = 30;
		
		public function House(name:String=null)	{
			super(name);
			
			//生成box底部的矩形各顶点
			addRectangleVertices(houseWidth, houseLength, 0, "box_bottom");
			//生成box顶部的矩形各顶点
			addRectangleVertices(houseWidth, houseLength, wallsHeight, "box_top");
			//生成roof底部的矩形各顶点
			addRectangleVertices(roofBottomWidth, roofBottomLength, wallsHeight, "roof_bottom");
			//生成roof顶部的矩形各顶点
			addRectangleVertices(roofTopWidth, roofTopLength, wallsHeight + roofHeight, "roof_top");
			
			
			// Create faces
			//wall面数组	
			var wallsFaces:Array = [];
			//roof面数组
			var roofFaces:Array = [];
			//roof底面数组
			var roofBottomFaces:Array = [];
			//为XY平面的每一象限创建面
			for (var i:int = 0; i < 4; i++) {
				//用与运算来确定j值
				var j:int = (i + 1) & 3;
				//创建wall面，并存入数组中以备之后生成表面
				//前三个参数为所需顶点，第四个为面片的名字
				wallsFaces.push(createFace(["box_bottom_" + i, "box_bottom_" + j, "box_top_" + j, "box_top_" + i], "wall_" + i));
				//创建roof斜面，并存入数组中以备之后生成表面
				roofFaces.push(createFace(["roof_bottom_" + i, "roof_bottom_" + j, "roof_top_" + j, "roof_top_" + i], "roof_slope_" + i));
				//创建roof底面，并存入数组中以备之后生成表面
				roofBottomFaces.push(createFace(["roof_bottom_" + j, "roof_bottom_" + i, "box_top_" + i, "box_top_" + j], "roof_bottom_" + i));
			}
			//创建roof顶面
			roofFaces.push(createFace(["roof_top_3", "roof_top_0", "roof_top_1", "roof_top_2"], "roof_top"));
			//创建box底面
			createFace(["box_bottom_3", "box_bottom_2", "box_bottom_1", "box_bottom_0"], "box_bottom");
			
			
			//创建表面。这里我们用到了之前建好的点数组
			//创建wall表面，参数分别为面数组，表面名字
			createSurface(wallsFaces, "walls");
			//创建roof斜表面和顶表面
			createSurface(roofFaces, "roof");
			//创建roof底表面
			createSurface(roofBottomFaces, "roof_bottom");
			//创建box底表面
			createSurface(["box_bottom"], "box_bottom");
			setMaterials();
		}
		
		/**
		 * Create vertices of a rectangle in the mesh
		 * 矩形宽度沿X方向，长度沿Y轴方向
		 * 参数依次为：矩形宽度，矩形长度，矩形Z坐标，点的名字
		 * 
		 */
		private function addRectangleVertices(rectWidth:Number, rectLength:Number, rectZ:Number, idPrefix:String):void {
			var hw:Number = rectWidth / 2;
			var hl:Number = rectLength / 2;
			//添加第一象限里的点
			createVertex(hw, hl, rectZ, idPrefix + "_0");
			//添加第二象限里的点
			createVertex(-hw, hl, rectZ, idPrefix + "_1");
			//添加第三象限里的点
			createVertex(-hw, -hl, rectZ, idPrefix + "_2");
			//添加第四象限里的点
			createVertex(hw, -hl, rectZ, idPrefix + "_3");
			
		}
		
		/**
		 * Assign materials to surfaces
		 */
		protected function setMaterials():void {
			//设置表面材质。我们使用线框材质以保持房子简洁。
			//把wall设置为白色
			setMaterialToSurface(new WireMaterial(1, 0xFFFFFF), "walls");
			//把roof设置为黄色
			setMaterialToSurface(new WireMaterial(1, 0xFFFF00), "roof");
			//把roof bottom这是为红色
			setMaterialToSurface(new WireMaterial(1, 0xFF0000), "roof_bottom");
			//把box bottom设置为绿色
			setMaterialToSurface(new WireMaterial(1, 0x00FF00), "box_bottom");
			
		}
	}
}